package nowcoder.written_examination.qunaer;

import org.junit.Test;

import java.util.LinkedList;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author fance
 * @date 18-7-4 上午7:10
 */
public class Solution {
    public boolean chkLegal(String A) {
        // write code here
        if (A == null || A.length() < 1) {
            return true;
        }
        LinkedList<Character> stack = new LinkedList<>();
        try {

            for (int i = 0; i < A.length(); i++) {
                char c = A.charAt(i);
                if (c == ']' || c == ')' ||
                        c == '}') {
                    stack.pop();
                }
                if (c == '[' || c == '(' ||
                        c == '{') {
                    stack.push(c);
                }
            }
        } catch (Exception e) {
            return false;
        }
        return stack.isEmpty();
    }
    public String formatString(String A, int n, char[] arg, int m) {
        // write code here
        int cnt = 0;
        for (int i = 0; i < A.length() - 1; i++) {
            if (A.charAt(i) == '%' && A.charAt(i + 1) == 's') {
                cnt++;
            }
        }
        int diff = arg.length - cnt;
        if (diff > 0) {
            for (int i = arg.length - diff; i < arg.length; i++) {
                A += arg[i];
            }
        }
        int index = 0;
        while (index < cnt) {
            A = A.replaceFirst("%s","" + arg[index++]);
        }
        return A;
    }
    // 复杂了.
    public int[] containKeyword(String[] A, int n, String[] keys, int m) {
        // write code here
        Set<Integer> set = new TreeSet<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (A[i].contains(keys[j])) {
                    set.add(i);
                }
            }
        }
        if (set.size() < 1) {
            return new int[] {-1};
        } else {
            int[] res = new int[set.size()];
            int i  = 0;
            for (Integer num : set) {
                res[i++] = num;
            }
            return res;
        }
    }



    @Test
    public void test() {
       String a =  "A%sC%sE";
       char[] arg = {'B','D','F'};
        System.out.println(formatString(a,7,arg,0));
    }

}
